//
//  CaptureViewController.m
//  LLP
//
//  Created by Porcellio on 10/13/12.
//  Copyright (c) 2012 Porcellio. All rights reserved.
//

#import "CaptureViewController.h"

#import "LLPScrollItem.h"
#import "LLPGridView.h"

@interface CaptureViewController ()

- (void)dismissEvent;
- (void)caputeEvent;
- (void)changeCameraEvent;

- (void)didClickItem:(id)sender;

@end

@implementation CaptureViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = @"拍摄";
    
//    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"
//                                                                       style:UIBarButtonItemStylePlain
//                                                                      target:self
//                                                                      action:@selector(dismissEvent)];
//    self.navigationItem.leftBarButtonItem = backButtonItem;
//    [backButtonItem release];
    
    if([LLPCaptureHelper canSupportCapture] && !TARGET_IPHONE_SIMULATOR) {
        
        _captureHelper = [[LLPCaptureHelper alloc] initWithCategory:LLPCaptureCategoryPhoto];
        _captureHelper.delegate = self;
        
        _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureHelper.session];
        _previewLayer.frame = CGRectMake(0.0f, 0.0f, 320.0f, 416.0f);
        _previewLayer.videoGravity = AVLayerVideoGravityResize;
        [self.view.layer addSublayer:_previewLayer];
        [_previewLayer release];
        
        UIButton *btnCapture = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnCapture setImage:[UIImage imageNamed:@"red-camera-icon.png"] forState:UIControlStateNormal];
        [btnCapture setFrame:CGRectMake(128.0f, 340.0f, 64.0f, 64.0f)];
        [btnCapture addTarget:self action:@selector(caputeEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btnCapture];
        
        UIButton *btnChangeCamera = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnChangeCamera setImage:[UIImage imageNamed:@"red-camera-icon.png"] forState:UIControlStateNormal];
        [btnChangeCamera setFrame:CGRectMake(10.0f, 10.0f, 32.0f, 32.0f)];
        [btnChangeCamera addTarget:self action:@selector(changeCameraEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btnChangeCamera];
        
        [_captureHelper.session startRunning];
    }
    
    _scrollView = [[LLPGridView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 416.0f)];
    [((LLPGridView *)_scrollView) setColumnCount:5];
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
    UIImage *image = [UIImage imageNamed:@"red-camera-icon.png"];
    LLPScrollItem *item;
    for (int i = 0; i < 50; i++) {
        item = [[LLPScrollItem alloc] initWithImage:image target:self selector:@selector(didClickItem:)];
        [_scrollView addItem:item];
        [item release];
        item = nil;
    }
}

- (void)dealloc {
    
    if(_captureHelper != nil) {
    
        [_captureHelper.session stopRunning];
    }
    
    [_captureHelper release];
    
    _captureHelper = nil;

    [super dealloc];
}

#pragma mark -
#pragma mark Private Methods.

- (void)didClickItem:(id)sender {

}

- (void)dismissEvent {

    [self dismissModalViewControllerAnimated:YES];
}

- (void)caputeEvent {

    [_captureHelper captureImage];
}

- (void)changeCameraEvent {

    [_captureHelper changeCameraPosition];
}

#pragma mark -
#pragma mark LLPCaptureHelperDelegate Support.

- (void)didCaptureStillImage:(NSData *)imageData {

}

- (void)didChangeDevicePosition:(AVCaptureDevice *)device {

    
}

@end
